package com.panwenliang2008.eatwhat.ui.select

import androidx.lifecycle.*
import com.panwenliang2008.eatwhat.data.FoodRepository
import com.panwenliang2008.eatwhat.data.entity.Food
import kotlinx.android.synthetic.main.select_fragment.*
import kotlin.random.Random

class SelectViewModel internal constructor(
    private val foodRepository: FoodRepository,
    private val savedStateHandle: SavedStateHandle
) : ViewModel() {

    fun getAll() = foodRepository.getAll()

    var randomFood: LiveData<Food> = MutableLiveData()

    fun updateRandomFood(): LiveData<Food> {
        randomFood = randomFood.value.let {
            if (it == null) {
                foodRepository.getRandomFood(0)
            } else {
                foodRepository.getRandomFood(randomFood.value!!.foodId)
            }
        }

        return randomFood
    }

    fun getSelectedFoodSavedState(): LiveData<String> {
        return savedStateHandle.getLiveData("food_name", "")
    }

    fun setSelectedFoodSavedState(foodName: String?) {
        foodName.let {
            savedStateHandle.set("food_name", it)
        }
    }
}
